﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.Pager.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmCustormerInfoList : BaseForm
    {

        /// <summary>
        /// 查询类别 1：今日在住客人 2：今日预离客人
        /// </summary>
        public int searchType = 0;
        private string selectectedRoomNo = string.Empty;
        private string selectectedOrderStaus = string.Empty;
        public FrmCustormerInfoList()
        {
            InitializeComponent();
        }

        private void FrmCustormerInfoList_Load(object sender, EventArgs e)
        {
           // this.cmTimeType.SelectedIndex = 0;
            InitialControl();
            txtBeginTime.Value = DateTime.Now.Date;
            txtEndTime.Value = DateTime.Now.Date;
            cmStatus.SelectedIndex = 0;
            switch (searchType)
            {
                case 1://今日在住
                    //cmTimeType.SelectedText = "入住时间";
                    cmStatus.Text = "在店";
                    break;
                case 2: //今日预离
                    cmTimeType.SelectedText = "预离时间";
                    cmStatus.Text = "在店";
                    break;

            }
            this.winGridView1.DisplayColumns = "RoomNo,Name,IDCarType,IDNumber,CreateDate,PreOffDate,Persons,PreLivedDays,DepositMoney,CheckOutDate,BillNumber,Status,Note";
            BindData();
        
        }

        private void InitialControl()
        {

            this.winGridView1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridView1.AppendedMenu = this.contextMenuStrip1;
            this.winGridView1.dataGridView1.MouseDown += new MouseEventHandler(dataGridView1_MouseDown);
            this.winGridView1.dataGridView1.MultiSelect = false;
            this.winGridView1.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
            //this.winGridView1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            //this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            #region 添加别名解析
            this.winGridView1.AddColumnAlias("RoomNo", "房间号");
            //this.winGridView1.AddColumnAlias("CompanyName", "公司名称");
            this.winGridView1.AddColumnAlias("Name", "宾客姓名");
            this.winGridView1.AddColumnAlias("Telephone", "联系电话");
            this.winGridView1.AddColumnAlias("Sex", "性别");
            this.winGridView1.AddColumnAlias("IDCarType", "证件类型");
            this.winGridView1.AddColumnAlias("IDNumber", "证件编号");
            //this.winGridView1.AddColumnAlias("Source", "宾客来源");
            this.winGridView1.AddColumnAlias("Persons", "人数");
            this.winGridView1.AddColumnAlias("DepositMoney", "押金");
            this.winGridView1.AddColumnAlias("PreLivedDays", "预住天数");
            this.winGridView1.AddColumnAlias("Status", "当前状态");
            this.winGridView1.AddColumnAlias("CreateDate", "入住时间");
            this.winGridView1.AddColumnAlias("PreOffDate", "预离时间");
            this.winGridView1.AddColumnAlias("CheckOutDate", "离店时间");
            this.winGridView1.AddColumnAlias("BillNumber", "结账单号");
            this.winGridView1.AddColumnAlias("Address", "宾客地址");
            this.winGridView1.AddColumnAlias("Note", "备注");
            #endregion
        
        
        }

       

        void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (this.winGridView1.dataGridView1.CurrentRow != null)
            {



                selectectedRoomNo = this.winGridView1.dataGridView1.CurrentRow.Cells["RoomNo"].Value.ToString();
                selectectedOrderStaus =this.winGridView1.dataGridView1.CurrentRow.Cells["Status"].Value.ToString();
            
            }



        }

        private void BindData()
        {
            SearchCondition condition = new SearchCondition();
            condition.AddCondition("RoomNo", txtRoomNo.Text, SqlOperator.Equal, true);
            condition.AddCondition("Name", txtUserName.Text, SqlOperator.Like, true);
            condition.AddCondition("IDNumber", txtCardNum.Text, SqlOperator.Like, true);

          
            int IsCheckOut = 0;
            if (cmStatus.Text != "全部")
            {
                if (cmStatus.Text == "在店")
                {
                    IsCheckOut = 0;
                }
                else
                {
                    IsCheckOut = 1;
                 
                }

                condition.AddCondition("IsCheckOut", IsCheckOut, SqlOperator.Equal, true);

            }

           

          
            
            if (cmTimeType.Text == "入住时间")
            {
                condition.AddCondition("CreateDate", txtBeginTime.Text, SqlOperator.MoreThanOrEqual,true);
                condition.AddCondition("CreateDate", txtEndTime.Text + " 23:59:00", SqlOperator.LessThanOrEqual, true);

            }
            else if (cmTimeType.Text == "预离时间")
            {
                condition.AddCondition("PreOffDate", txtBeginTime.Text, SqlOperator.MoreThanOrEqual, true);
                condition.AddCondition("PreOffDate", txtEndTime.Text + " 23:59:00", SqlOperator.LessThanOrEqual, true);

            }
            else if (cmTimeType.Text == "离店时间")
            {

                condition.AddCondition("CheckOutDate", txtBeginTime.Text, SqlOperator.MoreThanOrEqual, true);
                condition.AddCondition("CheckOutDate", txtEndTime.Text + " 23:59:00", SqlOperator.LessThanOrEqual, true);

            
            }
               
            string strcondition = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            DataTable dt = BLLFactory<Customer>.Instance.GetCustomerReport(strcondition);
            this.winGridView1.DataSource = dt.DefaultView;
            this.winGridView1.PrintTitle = Portal.gc.gAppUnit + " -- " + "来宾信息查询报表";

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            searchType =-1;
            BindData();



        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

     

        /// <summary>
        /// 结账退房
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_CheckOut_Click(object sender, EventArgs e)
        {


            if (selectectedRoomNo != null)
            {
                RoomInfo roomInfo = BLLFactory<Room>.Instance.FindByRoomNo(selectectedRoomNo);
                if (roomInfo.Status == Entity.RoomStatus.住净房)
                {
                    FrmRoomCheckOut dlg = new FrmRoomCheckOut();
                    dlg.RoomInfo = roomInfo;
                    dlg.ShowDialog();
                    BindData();
                }


            }
        }


        /// <summary>
        /// 部分结账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_CheckPart_Click(object sender, EventArgs e)
        {


            if (selectectedRoomNo != null)
            {
                RoomInfo roomInfo = BLLFactory<Room>.Instance.FindByRoomNo(selectectedRoomNo);
                FrmPartCheckOut dlg = new FrmPartCheckOut();
                dlg.RoomInfo = roomInfo;
                dlg.ShowDialog();
                BindData();
            }
        }

        /// <summary>
        /// 续住
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_ModifyDeposit_Click(object sender, EventArgs e)
        {


            if (selectectedRoomNo != null)
            {
                RoomInfo roomInfo = BLLFactory<Room>.Instance.FindByRoomNo(selectectedRoomNo);
                FrmEditDeposit dlg = new FrmEditDeposit();
                dlg.RoomInfo = roomInfo;
                if (dlg.ShowDialog() == DialogResult.OK)
                {

                    BindData();

                }
               
            }
        }

        /// <summary>
        /// 修改登记信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_ModifyRegister_Click(object sender, EventArgs e)
        {

            if (selectectedRoomNo != null)
            {
                RoomInfo roomInfo = BLLFactory<Room>.Instance.FindByRoomNo(selectectedRoomNo);
                FrmEditRoomRegister dlg = new FrmEditRoomRegister();
                dlg.RoomInfo = roomInfo;
                dlg.ShowDialog();
                BindData();
            }
        }

        /// <summary>
        /// 换房
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_SwithRoom_Click(object sender, EventArgs e)
        {


            if (selectectedRoomNo != null)
            {
                RoomInfo roomInfo = BLLFactory<Room>.Instance.FindByRoomNo(selectectedRoomNo);
                FrmSwitchRoom dlg = new FrmSwitchRoom();
                dlg.RoomInfo = roomInfo;
                if (dlg.ShowDialog() == DialogResult.OK)
                {

                    BindData();

                }
            }

        }

        /// <summary>
        /// 消费入账
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menu_AddConsume_Click(object sender, EventArgs e)
        {


            if (selectectedRoomNo != null)
            {
                RoomInfo roomInfo = BLLFactory<Room>.Instance.FindByRoomNo(selectectedRoomNo);
                FrmAddConsume dlg = new FrmAddConsume();
                dlg.RoomInfo = roomInfo;
                dlg.ShowDialog();
                BindData();
            }
          


        }


        void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button.ToString() == "Right")
            {
                bool enable = false;
                if (selectectedOrderStaus == "在店")
                {

                    enable = true;

                }

                menu_AddConsume.Enabled = enable;
                menu_CheckOut.Enabled = enable;
                menu_CheckPart.Enabled = enable;
                menu_ModifyDeposit.Enabled = enable;
                menu_SwithRoom.Enabled = enable;
                menu_SwithRoom.Enabled = enable;




            }
        }

       

       
    }
}
